<template>
  <!-- DCS数据点管理 -->
  <basic-container>

    <!-- 表格内容 -->
    <avue-crud 
      :option="option"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      v-model="form"
      ref="crud"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @row-del="rowDel"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >

      <!-- 按钮 -->
      <template slot="menuLeft">
        
        <!-- 删除 -->
        <el-button 
          type="danger"
          size="small"
          icon="el-icon-delete"
          plain
          v-if="permission.dcsdatasiteinfo_delete"
          @click="handleDelete">
          删 除
        </el-button>
        
        <!-- 导入 -->
        <el-button 
          type="success" 
          size="small" 
          plain 
          icon="el-icon-upload2" 
          @click="handleImport">
          导 入
        </el-button>

        <!-- 断开监控 -->
        <el-button 
          type="warning" 
          size="small" 
          plain 
          icon="el-icon-circle-close" 
          @click="disConnectMonitor">
          断开监控
        </el-button>

        <!-- 同步监控信息 -->
        <el-button 
          type="success" 
          size="small" 
          plain 
          icon="el-icon-refresh-left" 
          @click="synMonitor">
          同步监控信息
        </el-button>

      </template>

      <!-- 自定义状态 -->
      <template slot="zt" slot-scope="scope">
        <div v-if="scope.row['zt'] === 1">在线</div>
        <div v-else>下线</div>
      </template>

    </avue-crud>

    <!-- 导入弹窗 -->
    <el-dialog 
      title="DCS数据点导入" 
      append-to-body 
      :visible.sync="excelBox" 
      width="555px"
    >
      <avue-form 
        :option="excelOption" 
        v-model="excelForm" 
        :upload-after="uploadAfter" 
        :upload-before="beforeUpload"
        :upload-error="uploadError"
      >
        <template slot="excelTemplate">
          <el-button type="primary" @click="handleTemplate">
            点击下载<i class="el-icon-download el-icon--right"></i>
          </el-button>
        </template>
      </avue-form>
    </el-dialog>

  </basic-container>
</template>

<script>
import {getList, getDetail, add, update, remove, disConnectDcs, synMonitorDcs} from "@/api/configdb/dcspoint";
import {mapGetters} from "vuex";

export default {
  data() {
    return {
      excelBox: false,
      excelForm: {},
      excelOption: {
        submitBtn: false,
        emptyBtn: false,
        column: [{
            label: '模板上传',
            prop: 'excelFile',
            type: 'upload',
            drag: true,
            loadText: '模板上传中，请稍等',
            showFileList: false,
            span: 24,
            propsHttp: {
              fileName: 'multipartFile',
              res: 'data'
            },
            tip: '请上传 .xls,.xlsx 标准格式文件',
            action: "/api/configuration/dcsdatasiteinfo/import ",
            headers: {}
          },
          {
            label: '模板下载',
            prop: 'excelTemplate',
            formslot: true,
            span: 24,
          }
        ]
      },
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: this.$pageSize,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        index: true,
        viewBtn: true,
        selection: true,
        dialogClickModal: false,
        refreshBtn: false,
        columnBtn: false,
        column: [
          {
            label: "点位标签",
            prop: "dwbq",
            align: "center",
            search: true,
            rules: [{
              required: true,
              message: "请输入点位标签",
              trigger: "blur"
            }]
          },
          {
            label: "点位名称",
            prop: "dwmc",
            align: "center",
            search: true,
            rules: [{
              required: true,
              message: "请输入点位名称",
              trigger: "blur"
            }]
          },
          {
            label: "点位类型",
            prop: "dwlx",
            align: "center",
            type: 'select',
            dicUrl: "/api/blade-system/dict-biz/dictionary?code=dcs_data_site_type",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            rules: [{
              required: true,
              message: "请选择点位类型",
              trigger: "blur"
            }]
          },
          {
            label: "最大值",
            prop: "zdfz",
            align: "center",
            type: "number",
            rules: [{
              required: true,
              message: "请输入最大值",
              trigger: "blur"
            }]
          },
          {
            label: "最小值",
            prop: "zxfz",
            align: "center",
            type: "number",
            rules: [{
              required: true,
              message: "请输入最小值",
              trigger: "blur"
            }]
          },
          {
            label: "单位",
            prop: "dw",
            align: "center",
            rules: [{
              required: true,
              message: "请输入单位",
              trigger: "blur"
            }]
          },
          {
            label: "状态",
            prop: "zt",
            align: "center",
            slot: true,
            addDisplay: false,
            editDisplay: false,
            viewDisplay: false
          }
        ]
      },
      data: [],
      isFailed: false
    };
  },
  computed: {
    ...mapGetters(["permission"]),

    // 表格中按钮权限
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.dcsdatasiteinfo_add, false),
        viewBtn: this.vaildData(this.permission.dcsdatasiteinfo_view, false),
        delBtn: this.vaildData(this.permission.dcsdatasiteinfo_delete, false),
        editBtn: this.vaildData(this.permission.dcsdatasiteinfo_edit, false)
      };
    },

    // 勾选项主键
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }

  },
  methods: {

    // 弹出导入框
    handleImport() {
      this.excelBox = true;
    },

    // 导入前判断
    beforeUpload(file, done, loading) {
      this.isFailed = false;
      var testmsg = file.name.substring(file.name.lastIndexOf('.') + 1)
      const extension = testmsg === 'xls'
      const extension2 = testmsg === 'xlsx'
      if(!extension && !extension2) {
        this.$message.warning('请上传 .xls,.xlsx 标准格式文件!');
        loading()
      } else {
        done()
      }
    },
    
    // 模板下载
    handleTemplate() {
      let str = this.$UploadUrl + '/hg/file/template/dcsTemplate.xls';
      window.open(str);
    },

    // 导入失败后
    uploadError(error, column) {
      console.log(error,column);
      this.isFailed = true;
    },
    
    // 导入成功后
    uploadAfter(res, done) {
      done();
      if(this.isFailed === true) {
        return;
      }
      this.excelBox = false;
      this.$message.success("上传成功");
      this.cancle();
    },
    
    // 取消
    cancle() {
      this.page.currentPage = 1;
      this.onLoad(this.page);
    },
    
    // 断开监控
    disConnectMonitor() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm('确认断开监控吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({
          lock: true,
          text: 'Loading',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
        disConnectDcs(this.ids).then(() => {
          this.$message.success("断开成功");
          loading.close();
          this.page.currentPage = 1;
          this.onLoad(this.page);
        }).catch(() => {
          this.$message.error("断开失败");
          loading.close()
        })
      });
    },

    // 同步监控信息
    synMonitor() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm('确认同步监控吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({
          lock: true,
          text: 'Loading',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
        synMonitorDcs(this.ids).then(() => {
          this.$message.success("同步监控成功");
          loading.close();
          this.page.currentPage = 1;
          this.onLoad(this.page);
        }).catch(() => {
          loading.close()
          this.$message.error("同步监控失败");
        })
      });
    },
    
    // 新增
    rowSave(row, done, loading) {
      add(row).then(() => {
        this.onLoad(this.page);
        this.$message.success("操作成功!");
        done();
      }, error => {
        loading();
        window.console.log(error);
      });
    },

    // 编辑
    rowUpdate(row, index, done) {
      update(row).then(() => {
        this.onLoad(this.page);
        this.$message.success("操作成功!");
        done();
      });
    },

    // 删除一条
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        return remove(row.id);
      }).then(() => {
        this.onLoad(this.page);
        this.$message.success("操作成功!");
      });
    },

    // 删除多条
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        return remove(this.ids);
      }).then(() => {
        this.onLoad(this.page);
        this.$message.success("操作成功!");
        this.$refs.crud.toggleSelection();
      });
    },

    // 打开弹窗前
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        getDetail(this.form.id).then(res => {
          this.form = res.data.data;
        });
      }
      done();
    },

    // 清空搜索条件
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },

    // 搜索条件改变事件
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },

    // 勾选
    selectionChange(list) {
      this.selectionList = list;
    },

    // 清空勾选
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },

    // 换页数
    currentChange(currentPage){
      this.page.currentPage = currentPage;
      this.onLoad(this.page);
    },

    // 换每页显示数量
    sizeChange(pageSize){
      this.page.pageSize = pageSize;
      this.onLoad(this.page);
    },

    // 刷新
    refreshChange() {
      this.onLoad(this.page, this.query);
    },

    // 获取表格数据
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    }

  }
};
</script>

<style>
</style>